package com.example.demo.server.common;

import com.example.demo.ds.MyHashMap;
import com.example.demo.server.dao.LoginInfoDao;
import com.example.demo.server.model.LoginInfo;
import com.example.demo.server.model.UserInfo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;


public class SessionUtils {

    private static LoginInfoDao loginInfoDao = new LoginInfoDao();

     public static String getSessionId() {
         File file = new File(APPVariable.ROOT);
         if(!file.exists()) {
             return null;
         }
         // 通过读取“cookie”获取 sessionId
         return readAllFromFile(APPVariable.ROOT);
     }


    //读文件操作
    public static String readAllFromFile(String filePath) {
        byte[] bytes = new byte[0];
        try {
            bytes = Files.readAllBytes(Paths.get(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(bytes);
    }

    public static UserInfo getUser() {
        // 通过读取“cookie”获取 sessionId
        String sessionId = getSessionId();
        if(sessionId == null) {
            return null;
        }
        // 通过sessionId 获取 session
        String session = loginInfoDao.getSession(sessionId);
        if(session == null) {
            return null;
        }
        // 将 session 转化为 map
        MyHashMap<String, Object> sessionAttribution = ObjectMapper.jsonToMap(session);
        // 获取mapper里的键值
        return (UserInfo) sessionAttribution.get(APPVariable.SESSION_KEY);
    }

    public static void setUser(UserInfo userInfo) {
        File file = new File(APPVariable.ROOT);
        if(!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try(Writer writer = new FileWriter(APPVariable.ROOT)) {
            // 生成sessionId，存到cookie中
            String sessionId = UUID.randomUUID().toString().replaceAll("-", "");
            writer.write(sessionId);
            // 将session_key和当前用户信息打包成 map
            MyHashMap<String, Object> sessionAttribution = new MyHashMap<>();
            sessionAttribution.put(APPVariable.SESSION_KEY, userInfo);
            // map转化为session
            String session = ObjectMapper.mapToJson(sessionAttribution);
            // 打包成loginInfo
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setSessionId(sessionId);
            loginInfo.setSession(session);
            loginInfoDao.setSession(loginInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void removeCookie() {
        File file = new File(APPVariable.ROOT);
        if(file.exists()) {
            file.delete();
        }
    }

    public static void removeSession() {
         String sessionId = getSessionId();
         if(sessionId != null) {
             loginInfoDao.removeSession(sessionId);
         }
    }

}